'use strict';
const dayjs = require("dayjs");
module.exports = app => {
  const { INTEGER, STRING, TEXT, DECIMAL } = app.Sequelize;
  const Staff = app.model.define(
      'staff',
      {
            id: {type: INTEGER, primaryKey: true, autoIncrement: true},
            username: STRING(255),
            nickname: STRING(255),
            status: INTEGER,
            balance: DECIMAL(10, 2),
          total_balance: DECIMAL(10, 2),
            avatar_url: STRING(255),
            password: STRING(255),
              is_online: INTEGER,
              sale_count: INTEGER,
              settlement_ratio: DECIMAL(10, 2),
          create_time: {
                type: app.Sequelize.DATE,
                allowNull: true,
                get() {
                    return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
                }
            },
            updated_time: {
                type: app.Sequelize.DATE,
                allowNull: true,
                get() {
                    return dayjs(this.getDataValue('updated_time')).format('YYYY-MM-DD HH:mm:ss');
                }
            }
      },
      {
          tableName: 'staff',
          timestamps: true, // 启用时间戳(createdAt, updatedAt)
          updatedAt: 'updated_time', // 不想要 updatedAt
          createdAt: 'create_time', // 想要  createdAt 但是希望名称叫做 created_at
      }
  );
    Staff.associate = function() {
        // 接了多少订单
        app.model.Staff.hasMany(app.model.Order, { foreignKey: 'staff_id', targetKey: 'id' });
    }
  return Staff
};
